package top.withwings.bonus.bonus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.withwings.bonus.bonus.pojo.Good;
import top.withwings.bonus.bonus.pojo.GoodWithRule;
import top.withwings.bonus.bonus.pojo.bonus.rule.ExpirationCloseBonusRule;
import top.withwings.bonus.bonus.pojo.bonus.rule.HighPriceBonusRule;
import top.withwings.bonus.bonus.service.GoodService;
import top.withwings.bonus.bonus.service.RuleService;
import top.withwings.bonus.common.pojo.PageParam;
import top.withwings.bonus.common.pojo.PagedData;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    @Autowired
    private RuleService ruleService;

    @RequestMapping("/page")
    public PagedData<Good> getGoodPage(PageParam pageParam,@RequestParam(required =false,defaultValue="") String keyword,
                                                                @RequestParam(required =false,defaultValue="false") boolean enabledOnly){


        return goodService.getGoodPage(pageParam, keyword,enabledOnly);


    }

    @RequestMapping("/pageWithRule")
    public PagedData<GoodWithRule> getGoodPageWithRule(PageParam pageParam, @RequestParam(required =false,defaultValue="") String keyword,
                                                       @RequestParam(required =false,defaultValue="false") boolean enabledOnly){


        PagedData<Good> goodPage= goodService.getGoodPage(pageParam, keyword,enabledOnly);

        if(goodPage.getTotal() == 0){
            return PagedData.emptyPage();
        }else{
            List<GoodWithRule> goodsWithRule = new ArrayList<>();
            for (Good good : goodPage.getData()) {
                HighPriceBonusRule highPriceBonusRule = ruleService.findHighPriceRule(good.getId());
                ExpirationCloseBonusRule expirationCloseBonusRule = ruleService.findCloseExpirationRule(good.getId());
                goodsWithRule.add( new GoodWithRule(good,highPriceBonusRule,expirationCloseBonusRule));
            }
            return PagedData.create(goodsWithRule,goodPage.getTotal());
        }

    }

    @PostMapping("enable")
    public void enable(int goodId){

        Good good = goodService.findById(goodId);
        good.setEnabled(true);
        goodService.update(good);

    }

    @PostMapping("disable")
    public void disable(int goodId){

        Good good = goodService.findById(goodId);
        good.setEnabled(false);
        goodService.update(good);
    }

    @PostMapping("update")
    public void update(@RequestBody Good good){

        Good old = goodService.findById(good.getId());
        good.setEnabled( old.isEnabled());
        goodService.update(good);

    }


    @PostMapping("create")
    public void create(@RequestBody Good good){
        good.setEnabled(true );
        goodService.create(good);

    }


    @PostMapping("delete")
    public void delete(int id){

        goodService.delete(id);

    }


}
